import HyperDown from 'static/js/Parser'
import hljs from 'static/js/highlight.js'
import 'katex'
import renderMathInElement from 'static/js/auto-render.min.js'
import jQuery from 'jquery'
let tocF = function (text) {
  let parser = new HyperDown()
  text = parser.makeHtml(text)
  jQuery(function () {
    jQuery('.blog-entry :header').each(function (i) {
      jQuery(this).attr('id', 'toc_' + i)
    })
    jQuery('.toc a').on('click', function () {
      var tocTop = jQuery(jQuery(this).attr('title')).offset().top - 60
      window.scrollTo(0, tocTop)
    })
  })
  if (text.includes('[TOC]')) {
    text = text.replace('[TOC]', '')
    let h = text.match(/<h[1-6]>([\s\S]*?)<\/h[1-6]>/g)
    let tocStr = '<h1>目录</h1>'
    h.forEach(function (v, i) {
      tocStr += `<a title="#toc_${i}" href='javascript:;'>${v}</a>`
    })
    return {text, tocStr}
  }
  return {text}
}

export {
  hljs,
  renderMathInElement,
  tocF
}
